using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Admin.NET.Application.Service.Common;

public class ToolHelperService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<EarlyWarning> _rep;

	public ToolHelperService(SqlSugarRepository<EarlyWarning> rep)
	{
		_rep = rep;
	}

	/// <summary>
	/// 生成某张表的种子数据
	/// </summary>
	/// <param name="tableName"></param>
	/// <returns></returns>
	[AllowAnonymous]
	[HttpGet("/tool/table")]
	public async Task<string> ToolHelperTable(string tableName)
	{
		object infoMsg;
		switch (tableName)
		{
		case "SysConfig":
			infoMsg = await _rep.Context.Queryable<SysConfig>().ToListAsync();
			break;
		case "SysDictData":
			infoMsg = await _rep.Context.Queryable<SysDictData>().ToListAsync();
			break;
		case "SysDictType":
			infoMsg = await _rep.Context.Queryable<SysDictType>().ToListAsync();
			break;
		case "SysMenuSeed":
			infoMsg = await _rep.Context.Queryable<SysMenu>().ToListAsync();
			break;
		case "SysOrg":
			infoMsg = await _rep.Context.Queryable<SysOrg>().ToListAsync();
			break;
		case "SysPos":
			infoMsg = await _rep.Context.Queryable<SysPos>().ToListAsync();
			break;
		case "SysRole":
			infoMsg = await _rep.Context.Queryable<SysRole>().ToListAsync();
			break;
		case "SysTenant":
			infoMsg = await _rep.Context.Queryable<SysTenant>().ToListAsync();
			break;
		case "SysUserExtOrg":
			infoMsg = await _rep.Context.Queryable<SysUserExtOrg>().ToListAsync();
			break;
		case "SysUserRole":
			infoMsg = await _rep.Context.Queryable<SysUserRole>().ToListAsync();
			break;
		case "SysUser":
			infoMsg = await _rep.Context.Queryable<SysUser>().ToListAsync();
			break;
		default:
			return "当前类型未配置种子数据";
		}
		string modelValue = "";
		if (infoMsg != null)
		{
			if (!(infoMsg is IList model))
			{
				return "";
			}
			foreach (object warning in model)
			{
				modelValue = modelValue + " new " + tableName + "() {";
				PropertyInfo[] propletys = warning.GetType().GetProperties();
				PropertyInfo[] array = propletys;
				foreach (PropertyInfo itemprop in array)
				{
					object propInfo = itemprop.GetValue(warning);
					modelValue = modelValue + GetValueByType(itemprop, propInfo) + ",";
				}
				modelValue = modelValue.Substring(0, modelValue.Length - 1);
				modelValue += "},\r\n";
			}
			string path = AppDomain.CurrentDomain.BaseDirectory + "TempClass/temp.txt";
			string msgClass = File.ReadAllText(path);
			msgClass = msgClass.Replace("#[name]#", tableName).Replace("#[data]#", modelValue.Substring(0, modelValue.Length - 1));
			if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + $"TempClass/{DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day}"))
			{
				Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + $"TempClass/{DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day}");
			}
			string pathoutput = AppDomain.CurrentDomain.BaseDirectory + $"TempClass/{DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day}/{tableName}SeedData.cs";
			File.WriteAllText(pathoutput, msgClass);
			return "生成成功【" + pathoutput + "】";
		}
		return "当前种子数据为空";
	}

	/// <summary>
	/// 生成所有表的种子数据
	/// </summary>
	/// <returns></returns>
	[AllowAnonymous]
	[HttpGet("/tool/tableall")]
	public async Task<string> ToolHelperTableAll()
	{
		List<string> list = new List<string>
		{
			"SysConfig", "SysDictData", "SysDictType", "SysMenuSeed", "SysOrg", "SysPos", "SysRole", "SysTenant", "SysUserExtOrg", "SysUserRole",
			"SysUser", "SysUserExtOrg", "SysUserExtOrg", "SysUserExtOrg"
		};
		foreach (string s in list)
		{
			await ToolHelperTable(s);
		}
		string pathoutput = AppDomain.CurrentDomain.BaseDirectory + $"TempClass/{DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day}";
		return "生成成功【" + pathoutput + "】";
	}

	private string GetValueByType(PropertyInfo type, object valueobj)
	{
		string valueInfo = type.Name + "=";
		if (valueobj == null)
		{
			return valueInfo + "null";
		}
		string value = valueobj.ToString();
		switch (type.PropertyType.ToString())
		{
		case "System.Nullable`1[System.String]":
		case "System.String":
			valueInfo = valueInfo + "\"" + value + "\"";
			break;
		case "System.Nullable`1[System.DateTime]":
		case "System.DateTime":
			valueInfo = valueInfo + "DateTime.Parse(\"" + value + "\")";
			break;
		case "System.Nullable`1[System.Boolean]":
		case "System.Boolean":
			valueInfo += value.ToLower();
			break;
		case "System.Nullable`1[System.Int64]":
		case "System.Nullable`1[System.Int16]":
		case "System.Nullable`1[System.Int32]":
		case "System.Nullable`1[System.Byte]":
		case "System.Int16":
		case "System.Int32":
		case "System.Int64":
		case "System.Byte":
			valueInfo += value;
			break;
		case "System.Nullable`1[System.Double]":
		case "System.Nullable`1[System.Decimal]":
		case "System.Decimal":
		case "System.Double":
			valueInfo += value;
			break;
		case "System.DBNull":
			valueInfo += value;
			break;
		default:
			valueInfo = ((!type.PropertyType.IsEnum) ? (valueInfo + value) : (valueInfo + type.PropertyType.Name + "." + value));
			break;
		}
		return valueInfo;
	}
}
